<template>
    <el-dialog :model-value="dialogVisible" :close-on-click-modal="true" :before-close="handleClose" :title="isAdd === 'add' ? '新增数据源表' : '修改数据源表'" append-to-body width="520px">
        <el-form ref="formRef" :model="form" :rules="rules" label-width="120px">
            <el-form-item label="数据源表名称" prop="sourceName">
                <el-input v-model="form.sourceName" placeholder="请输入数据源表名称" class="w100p"/>
            </el-form-item>
            <el-form-item label="数据源表code" prop="sourceCode">
                <el-input v-model="form.sourceCode" placeholder="请输入数据源表code" class="w100p"/>
            </el-form-item>
        </el-form>
        <template #footer>
            <span class="dialog-footer">
                <el-button :loading="loading" type="primary" @click="submit()">确定</el-button>
                  <el-button @click="handleClose">取消</el-button>
            </span>
        </template>
    </el-dialog>
</template>

<script  setup>
import config from '@/settings'
// eslint-disable-next-line no-unused-vars
const props = defineProps({
    dialogVisible: {
        type: Boolean,
        default: false
    },
    isAdd: {
        type: String,
        default: 'add'
    },
    formData: {
        type: Object,
        default: ()=>{}
    }
})

const emit = defineEmits(['on-submit', 'handleClose'])
const formRef = ref(null)
const form = reactive({})
const options = ref([])

const validatesourceCode = (rule, value, callback) => {
    let reg = /^[a-zA-Z]\w+$/
    let status = true
    const arr = config.validateLisOptions
    arr.forEach((item)=>{
        if(value === item){
            callback(new Error('不能为关键字'+item))
            status = false
        }
    })
    if (status) {
        if(!reg.test(value)){
            callback(new Error('26个英文字母和0-9的自然数+下划线组成，首字符为字母'))
        }else{
            callback()
        }   
    }
}

const rules = reactive({
    sourceName: [
        {
            required: true,
            message: '请输入数据源表名称',
            trigger: 'blur'
        }
    ],
    sourceCode: [
        {
            required: true,
            message: '请输入数据源表code',
            trigger: 'blur'
        },
        { validator: validatesourceCode, trigger: 'blur' }
    ]
})
const loading = ref(false)
const handleClose = () => {
    formRef.value.resetFields()
    emit('handleClose',false)
}
const submit = () => {
    if (!formRef) return
    formRef.value.validate(valid => {
        if (valid) {
            emit('on-submit', {...form})
            handleClose()
        } else {
            return false
        }
    })

}

</script>

<style lang="scss" scoped>
.dialog-footer{
    text-align: right;
}
</style>
